package com.bjwl.wybx.controller.safty;

import com.bjwl.wybx.VO.UserQueryVO;
import com.bjwl.wybx.VO.UserRegisterVO;
import com.bjwl.wybx.VO.UserUpdateVO;
import com.bjwl.wybx.exception.SysException;
import com.bjwl.wybx.service.UserService;
import com.bjwl.wybx.util.Constants;
import com.bjwl.wybx.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/safty/user")
public class UserController {
    @Autowired
    private UserService userService;
    //添加用户
    @PostMapping("/add")
    public Result add(@RequestBody UserRegisterVO userRegisterVO){
        System.out.println(userRegisterVO);
        try {
            int num = userService.add(userRegisterVO);
            if(num>0){
                return Result.success("注册成功");
            }
            return Result.fail("注册失败");
        } catch (SysException e) {
            e.printStackTrace();
            return Result.fail("系统升级中");
        }
    }
    //退出登录
    @GetMapping("/exit")
    public Result exit(HttpSession session){
        //移除sessionId
        try {
            session.removeAttribute(Constants.SESSION_ATTR_CURRUSER);
            return Result.success("您已退出");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统升级中，退出登陆失败！");
        }
    }
    //删除用户
    @DeleteMapping("/delete")
    public Result delete(Integer repaire_id){
        try {
            //删除用户表 和 用户角色表 中的值
            int num=userService.deleteUser(repaire_id);
            if(num>0){
                return Result.success("删除成功");
            }
            return Result.fail("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统升级中，删除失败！");
        }
    }
    //修改用户
    public Result update(UserUpdateVO userUpdateVO){
        try {
            int num=userService.updateUser(userUpdateVO);
            if(num>0){
                return Result.success("更新成功");
            }
            return Result.fail("更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统升级中，更新失败！");
        }
    }
    //查询用户
    public Result queryUserAll(){
        try {
            List<UserQueryVO> userList=userService.query();
            return Result.success("查询成功",userList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统升级中，查询失败");
        }
    }




}
